/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_hdi_test_entry.h"
#include "sle_adapter_hdi.h"
#include "sle_seek_hdi.h"
#include "sle_unit_test_log.h"

SleSeekHdiCallbacks *g_seekHdiUtCbs;
SleSeekHdiOpts *g_seekHdiUtOpts = NULL;

void OnSleHdiSeekEnableUtCallback(SleHdiErrorCode errCode)
{
    SLE_UT_LOG("[SleSeekHdiTest][PASSED]OnSleHdiSeekEnableUtCallback errCode(%08X)", errCode);
}

void OnSleHdiSeekDisableUtCallback(SleHdiErrorCode errCode)
{
    SLE_UT_LOG("[SleSeekHdiTest][PASSED]OnSleHdiSeekDisableUtCallback errCode(%08X)", errCode);
}

void OnSleHdiSeekResultUtCallback(SleSeekResult *seekResultData)
{
    SLE_UT_LOG("[SleSeekHdiTest][PASSED]OnSleHdiSeekResultUtCallback");
}

void GetSleSeekHdiOptsTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = GetSleSeekHdiOpts(g_seekHdiUtOpts);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleSeekHdiTest][FAILED]GetSleSeekHdiOptsTestLevel0 failed(%08X)", ret);
    SLE_UT_LOG("[SleSeekHdiTest][PASSED] GetSleSeekHdiOptsTestLevel0 pass");
}

void StartSeekHdiTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = g_seekHdiUtOpts->startSeek();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleSeekHdiTest][FAILED]StartSeekHdiTestLevel0 failed(%08X)", ret);
    SLE_UT_LOG("[SleSeekHdiTest][PASSED] StartSeekHdiTestLevel0 pass");

}

void StopSeekHdiTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = g_seekHdiUtOpts->stopSeek();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleSeekHdiTest][FAILED]StopSeekHdiTestLevel0 failed(%08X)", ret);
    SLE_UT_LOG("[SleSeekHdiTest][PASSED] StopSeekHdiTestLevel0 pass");
}
void SetSeekParamHdiTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    SleSeekParam param = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
        .repateFilter = false,
        .filterPolicy = SLE_SEEK_FILTER_ALL_ALLOW,
        .phys = SLE_SEEK_2M_PHY,
        .type[0] = SLE_ACTIVE_SEEK,
        .interval[0] = 100,
        .window[0] = 100,
    };
    ret = g_seekHdiUtOpts->setSeekParam(&param);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleSeekHdiTest][FAILED]SetSeekParamHdiTestLevel0 failed(%08X)", ret);
    SLE_UT_LOG("[SleSeekHdiTest][PASSED] SetSeekParamHdiTestLevel0 pass");
}

void SleHdiRegisterSeekCallbacksTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    SLE_UT_CHECK_AND_RETURN_LOG(g_seekHdiUtCbs != NULL, "[SleSeekHdiTest][FAILED]SleHdiRegisterSeekCallbacksTestLevel0 malloc failed");
    ret = g_seekHdiUtOpts->registerSeeCbks(g_seekHdiUtCbs);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleSeekHdiTest][FAILED]SleHdiRegisterSeekCallbacks failed(%08X)", ret);
    SLE_UT_LOG("[SleSeekHdiTest][PASSED] SleHdiRegisterSeekCallbacks pass");
}

void SleSeekHdiTestLevel0(void)
{
    GetSleSeekHdiOptsTestLevel0();
    SleHdiRegisterSeekCallbacksTestLevel0();
    SetSeekParamHdiTestLevel0();
    StartSeekHdiTestLevel0();
    sleep(1);
    StopSeekHdiTestLevel0();
    sleep(1);
}

void GetSleSeekHdiOptsTestLevel1(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    g_seekHdiUtOpts = NULL;
    ret = GetSleSeekHdiOpts(g_seekHdiUtOpts);
    SLE_UT_CHECK_AND_PRINT_LOG(ret != SLE_HDI_ERRCODE_SUCC, "[SleSeekHdiTest][FAILED]GetSleSeekHdiOptsTestLevel0 failed(%08X)", ret);
    SLE_UT_LOG("[SleSeekHdiTest][PASSED] GetSleSeekHdiOptsTestLevel0 pass");
}

SleHdiErrorCode SleSeekHdiTestLevel1(void)
{
    GetSleSeekHdiOptsTestLevel1();
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode InitSleSeekHdiTestEntry(void)
{
    g_seekHdiUtOpts = (SleSeekHdiOpts *)malloc(sizeof(SleSeekHdiOpts));
    SLE_UT_CHECK_AND_RETURN_RET_LOG(g_seekHdiUtOpts != NULL, SLE_HDI_ERRCODE_FAIL, "[SleSeekHdiTest][FAILED]InitSleSeekHdiTestEntry malloc failed");
    g_seekHdiUtCbs = (SleSeekHdiCallbacks *)malloc(sizeof(SleSeekHdiCallbacks));
    SLE_UT_CHECK_AND_RETURN_RET_LOG(g_seekHdiUtCbs != NULL, SLE_HDI_ERRCODE_FAIL, "[SleSeekHdiTest][FAILED]InitSleSeekHdiTestEntry malloc failed");
    g_seekHdiUtCbs->OnSeekStartCb = OnSleHdiSeekEnableUtCallback;
    g_seekHdiUtCbs->OnSeekStopCb = OnSleHdiSeekDisableUtCallback;
    g_seekHdiUtCbs->OnSeekResultCb = OnSleHdiSeekResultUtCallback;
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode DeinitSleSeekHdiTestEntry(void)
{
    free(g_seekHdiUtCbs);
    free(g_seekHdiUtOpts);
    return SLE_HDI_ERRCODE_SUCC;
}

void SleSeekHdiTestEntry(void)
{
    SLE_UT_LOG("__SleSeekHdiTestEntry start");
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = InitSleSeekHdiTestEntry();
    SLE_UT_CHECK_AND_RETURN(ret == SLE_HDI_ERRCODE_SUCC);
    SleSeekHdiTestLevel0();
    // SleSeekHdiTestLevel1();
    ret = DeinitSleSeekHdiTestEntry();
    SLE_UT_CHECK_AND_RETURN(ret == SLE_HDI_ERRCODE_SUCC);
    SLE_UT_LOG("__SleSeekHdiTestEntry end");
}